package com.timothy.android;

import java.util.ArrayList;
import java.util.List;

import com.timothy.android.util.XMLUtil;
import com.timothy.answer.STTwoButtonAlphaAnswer;
import com.timothy.question.Choice;
import com.timothy.question.QuestionBuilder;
import com.timothy.question.STTwoButtonAlpha;
import com.timothy.question.TypeBean1;
import com.timothy.question.TypeBean1Config;
import com.timothy.service.BackLightService;
import com.timothy.service.MessageSender;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class LayoutA extends BaseActivity{
	private static final String LOG_TAG = "LayoutA";
	
	private LinearLayout layoutA;
	private TextView tv_msg = null;
	private Button btnA = null;
	private Button btnB = null;
	
	private Button btnPre = null;
	private Button btnNext = null;
	
	private TypeBean1 bean;
	private TypeBean1Config beanConfig;
	
	private Drawable btnDrawable;
//	private Drawable layoutDrawable;
	
	//系统原本屏幕亮度
//	private float screenBrightness;
	
//	private Messenger rMessenger = new Messenger( new IncomingHandler() );
//    private Messenger sMessenger = null;
	
	STTwoButtonAlpha sTTwoButtonAlpha = null;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.layouta);
		
		//receive messager from PC
		Intent intent = getIntent();
		/*String message = intent.getStringExtra("STTwoButtonAlpha"); 
		String type = XMLUtil.getPropertyValue(message, "Type");
		if( "CommonQuestion".equals( type ) ) {
			int index = message.indexOf("<Content>")+"<Content>".length();
			int endIndex = message.indexOf("</Content>");
			String question = message.substring(index, endIndex);
			sTTwoButtonAlpha= (STTwoButtonAlpha) QuestionBuilder.parseQuestion( question );
		}*/
		
		//mock information
		sTTwoButtonAlpha = new STTwoButtonAlpha();
		List<Choice> mockChoices = new ArrayList<Choice>();
		sTTwoButtonAlpha.setQuestionName("Are you happy?");
		mockChoices.add(new Choice("Y","Yes"));
		mockChoices.add(new Choice("N","No"));
		sTTwoButtonAlpha.setChoices(mockChoices);
		
		List<Choice> choices = sTTwoButtonAlpha.getChoices();
		
		//mock receive data
		bean = TypeBean1.getMockInstance();
		beanConfig = bean.getConfig();
		
		//设置手机背景
		layoutA = (LinearLayout) this.findViewById(R.id.layoutA);
		layoutA.setBackgroundColor(Color.CYAN);
		
		//设置Global Prompt 
		tv_msg = (TextView) this.findViewById(R.id.promotMsg);
		tv_msg.setText(sTTwoButtonAlpha.getQuestionName());
//		tv_msg.setText(message);
		tv_msg.setTextColor(beanConfig.getLabelTextColor());
		tv_msg.setTextSize(beanConfig.getLabelTextSize());
		tv_msg.setBackgroundColor(beanConfig.getLabelBgColor());
		
		//设置ButtonA
		btnA = (Button) this.findViewById(R.id.btnA);
		btnA.setText(choices.get(0).getContent());
		btnA.setHeight(80);
		btnA.setTextColor(beanConfig.getButtonTextColor());
		btnA.setTextSize(beanConfig.getButtonTextSize());
		
		//设置ButtonB
		btnB = (Button) this.findViewById(R.id.btnB);
		btnB.setText(choices.get(1).getContent());
		btnB.setHeight(80);
		btnB.setTextColor(bean.getConfig().getButtonTextColor());
		btnB.setTextSize(bean.getConfig().getButtonTextSize());
		
	    //屏幕保持亮光(始终不锁屏)
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        
        //得到button背景图片
        Resources resources = this.getResources(); 
        btnDrawable = resources.getDrawable(R.drawable.button);
//        layoutDrawable = resources.getDrawable(R.drawable.layout);
//        layoutA.setBackgroundDrawable(btnDrawable);
				
		btnA.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				btnA.setBackgroundColor(Color.YELLOW);
				btnB.setBackgroundDrawable(btnDrawable); 

//				STTwoButtonAlphaAnswer answer = new STTwoButtonAlphaAnswer();
//				answer.setMark("Y");
//				answer.setQuestion(sTTwoButtonAlpha);
//				MessageSender.sendAnswer(answer);
			}
		});
		
		btnB.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {

				btnA.setBackgroundDrawable(btnDrawable);
				btnB.setBackgroundColor(Color.YELLOW);

//				STTwoButtonAlphaAnswer answer = new STTwoButtonAlphaAnswer();
//				answer.setMark("N");
//				answer.setQuestion(sTTwoButtonAlpha);
//				MessageSender.sendAnswer(answer);
			}
		});
		
		//往前或往后翻阅题目
		btnPre = (Button) this.findViewById(R.id.btnPre);
		btnPre.setEnabled(false);
		btnNext = (Button) this.findViewById(R.id.btnNext);
		btnNext.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
			    intent.setClass(LayoutA.this,LayoutB.class);
			    startActivity(intent);
			}
		});
		
//		// 记录手机原先屏幕亮度
//        WindowManager.LayoutParams lp = getWindow().getAttributes();     
//        screenBrightness = lp.screenBrightness;		      
//        Log.i(LOG_TAG,"Screen Start Brightness:" + screenBrightness);
        
//        doBindService();
	}

	@Override
	protected void processMessage(int what, String content) {
		// TODO Auto-generated method stub
		
	}
		
	
/*	//屏幕触摸事件
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		//恢复屏幕亮度
		Log.i(LOG_TAG,"Recover Screen Start Brightness.");
		Log.i(LOG_TAG,"Get Screen Start Brightness:" + getScreenStartBrightness());
		
		WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = getScreenStartBrightness();
        getWindow().setAttributes(lp);
        
        Message msg = Message.obtain(null, BackLightService.MSG_TOUCH_SCREEN);
        msg.replyTo = rMessenger;
        try {
        	sMessenger.send(msg);
        } catch (RemoteException e) {
        }
        
		return super.onTouchEvent(event);
	}

	//Activity 销毁时的操作
	protected void onDestroy() {
        super.onDestroy();
		//恢复屏幕亮度
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.screenBrightness = getScreenStartBrightness();
        getWindow().setAttributes(lp);
    }
	
	protected void onResume(){
		super.onResume();
		doBindService();
	}
	
	protected void onPause(){
		super.onPause();
		doUnbindService();
	}
	
	private boolean mIsBound;
	
	private void doBindService() {
        bindService(new Intent(LayoutA.this, BackLightService.class), mrsConnection, Context.BIND_AUTO_CREATE);
    }

    private void doUnbindService() {
        if (mIsBound) {
            unbindService(mrsConnection);
            mIsBound = false;
        }
    }
	
	private ServiceConnection mrsConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
        	 sMessenger = new Messenger(service);
             try {
                 Message msg = Message.obtain(null, BackLightService.MSG_REGISTER_CLIENT);
                 msg.replyTo = rMessenger;
                 sMessenger.send(msg);
             } catch (RemoteException e) {
             }
        
            mIsBound = true;
        }

        public void onServiceDisconnected(ComponentName className) {
        	 Message msg = Message.obtain(null, BackLightService.MSG_UNREGISTER_CLIENT);
             msg.replyTo = rMessenger;
             try {
            	 sMessenger.send(msg);
             } catch (RemoteException e) {
             }
        }
    };
    
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
        	Log.i(LOG_TAG,"msg.what:"+msg.what);
        	
            switch (msg.what) {
                case BackLightService.MSG_DARK:
		        	//使屏幕的亮度变暗
		        	WindowManager.LayoutParams lp = getWindow().getAttributes();
	        		lp.screenBrightness = 0.2f;
		            getWindow().setAttributes(lp);
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
  }*/
}
